<!-- Banner-->

<template>
  <el-dialog
    :model-value="dialogFormVisible"
    :destroy-on-close="true"
    title="Banner信息"
    width="600px"
    @close="emits('closeDialog')"
  >
    <el-form
      ref="dialogFormRef"
      :model="dialogForm"
      :rules="rules"
      :label-width="formLabelWidth"
    >
      <el-form-item label="Banner图片" :label-width="formLabelWidth">
        <el-upload
          :action="uploadimgUrl"
          :data="{ project_code: 'slotsgame' }"
          :show-file-list="false"
          :on-success="handleUploaderIconSuccess"
          :limit="1"
          :headers="TOKENT_HEADER"
          class="avatar-uploader"
        >
          <img v-if="dialogForm.cover" :src="dialogForm.src" class="avatar" />
          <el-icon v-else class="avatar-uploader-icon"><Plus /></el-icon>
        </el-upload>
      </el-form-item>
      <el-form-item label="名称" prop="name">
        <el-input v-model="dialogForm.name" />
      </el-form-item>
      <el-form-item label="跳转类型" prop="go_type">
        <el-select v-model="dialogForm.go_type" placeholder="跳转类型">
          <el-option label="go_app" value="go_app" />
          <el-option label="go_h5" value="go_h5" />
          <el-option label="go_game" value="go_game" />
        </el-select>
      </el-form-item>
      <el-form-item label="是否登陆" prop="is_login">
        <el-select v-model="dialogForm.is_login" placeholder="是否登陆">
          <el-option label="否" value="0" />
          <el-option label="是" value="1" />
        </el-select>
      </el-form-item>
      <el-form-item label="跳转url">
        <el-select v-model="dialogForm.url">
          <el-option
            v-for="item in bannerPageList"
            :key="item.id_name"
            :label="item.page_name"
            :value="item.id_name"
          />
        </el-select>
        注：名称字段值为 WebController 时，选择跳转url
        <!-- <el-input v-model="dialogForm.url" /> -->
      </el-form-item>
      <el-form-item label="游戏ID号">
        <el-input v-model="dialogForm.game_id" type="number" />
      </el-form-item>
      <el-form-item label="Banner位置">
        <el-input v-model="dialogForm.position" type="number" />
      </el-form-item>
    </el-form>
    <template #footer>
      <span class="dialog-footer">
        <el-button @click="emits('cancel')">取消</el-button>
        <el-button
          type="primary"
          @click="submit(dialogForm)"
          :loading="btnLoading"
        >
          提交
        </el-button>
      </span>
    </template>
  </el-dialog>
</template>
<script setup>
import { ref } from "vue";
import { uploadimgUrl } from "@/api/system/uploadimg";
import { TOKENT_HEADER } from "@/config/api-constant/system";
const dialogFormRef = ref(null);

defineProps({
  dialogFormVisible: {
    type: Boolean,
    default: false,
  },
  dialogForm: {
    type: Object,
    default: {
      cover: "",
      name: "",
      go_type: "",
      is_login: "",
      url: "",
      position: "",
      game_id: ""
    },
  },
  btnLoading: {
    type: Boolean,
    default: false,
  },
  bannerPageList: {
    type: Array,
    default: [],
  },
});
const emits = defineEmits([
  "closeDialog",
  "cancel",
  "submitForm",
  "handleUploaderImgRequest",
]);

const formLabelWidth = "120px";
// 校验
const rules = {
  name: [{ required: true, message: "请输入名字", trigger: "blur" }],
  go_type: [{ required: true, message: "请选择跳转类型", trigger: "blur" }],
  is_login: [{ required: true, message: "请选择是否登陆", trigger: "blur" }],
//  url: [{ required: true, message: "请输入URL", trigger: "blur" }],
};

// 提交
const submit = (dialogForm) => {
  dialogFormRef.value
    .validate()
    .then(async () => {
      try {
        emits("submitForm", dialogForm);
      } catch (error) {
        console.log(error);
      }
    })
    .catch((error) => {
      console.log(error);
    });
};
const handleUploaderIconSuccess = (res) => {
  emits("handleUploaderImgRequest", res, "cover");
};
</script>
